package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.Details0Bean2;
import com.wnhz.work.yz.utils.GlideImageLoader;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.youth.banner.Banner;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InstallDetails22Activity extends BaseActivity implements View.OnClickListener {
    private Banner banner;
    private TextView title, info, price, area, comnum, look, project_name;
    private RecyclerView commit;
    private EditText shop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_install_details22);
        initTitle();
        initView();
    }

    private int num = 0;

    private void initView() {
        findViewById(R.id.reduce).setOnClickListener(this);
        findViewById(R.id.ll_add).setOnClickListener(this);
        commit = (RecyclerView) findViewById(R.id.commit);
        commit.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        findViewById(R.id.select_project).setOnClickListener(this);
        findViewById(R.id.look).setOnClickListener(this);
        findViewById(R.id.look2).setOnClickListener(this);
        findViewById(R.id.btn).setOnClickListener(this);
        banner = (Banner) findViewById(R.id.banner);
        title = (TextView) findViewById(R.id.title);
        info = (TextView) findViewById(R.id.info);
        area = (TextView) findViewById(R.id.area);
        comnum = (TextView) findViewById(R.id.comnum);
        look = (TextView) findViewById(R.id.look);
        price = (TextView) findViewById(R.id.price);
        project_name = (TextView) findViewById(R.id.project_name);
        if ("5".equals(pid)) {
            // findViewById(R.id.senum).setVisibility(View.VISIBLE);
            findViewById(R.id.select).setVisibility(View.VISIBLE);
        }
        shop = (EditText) findViewById(R.id.shop);
        shop.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    num = Integer.parseInt(shop.getText().toString());
                } catch (Exception e) {
                    num = 0;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        upLoad();
    }

    private String pid = "";
    private String name = "";
    private String type = "";

    private void initTitle() {
        type = getIntent().getStringExtra("type");
        pid = getIntent().getStringExtra("pid");
        name = getIntent().getStringExtra("name");
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("" + name);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        ImageView ic_telephone = (ImageView) findViewById(R.id.ic_telephone);
        ic_telephone.setOnClickListener(this);
        ic_telephone.setVisibility(View.GONE);
    }

    private Details0Bean2.InfoEntity info1 = new Details0Bean2.InfoEntity();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("pid", pid);
        map.put("type", type);
        showDialog();
        XUtil.Post(Url.DECORATE_DETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        Details0Bean2 bean = gson.fromJson(result, Details0Bean2.class);
                        Details0Bean2.InfoEntity info = bean.getInfo();
                        info1 = info;
                        setData(info);
                    } else {
                        String info = jsonObject.optString("info");
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });

    }

    private TextView pingjia;

    private void setData(final Details0Bean2.InfoEntity info) {
        pingjia = (TextView) findViewById(R.id.pingjia);
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(info.getIndex_pic());
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        title.setText("" + info.getName());
        this.info.setText("" + info.getDes());
        price.setText("¥" + info.getPrice() + "/" + info.getUnit());
        pingjia.setText("用户评价：(" + info.getComment_nums() + ")");
        comnum.setText("" + info.getComment_nums());
        commit.setAdapter(new BaseRVAdapter(this, info.getComment()) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_comment2;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                ImageLoader.getInstance().displayImage(info.getComment().get(position).getHead_img(), holder.getImageView(R.id.head));
                holder.setTextView(R.id.name, info.getComment().get(position).getRealname());
                holder.setTextView(R.id.time, info.getComment().get(position).getAdd_time());
                holder.setTextView(R.id.info, info.getComment().get(position).getContent());


                final List<String> img = info.getComment().get(position).getImg();
                LinearLayout status1 = holder.getView(R.id.status1);
                LinearLayout status2 = holder.getView(R.id.status2);
                LinearLayout status3 = holder.getView(R.id.status3);
                ImageView img1 = holder.getImageView(R.id.img1);
                ImageView img2_1 = holder.getImageView(R.id.img2_1);
                ImageView img2_2 = holder.getImageView(R.id.img2_2);
                RecyclerView img_recycler = holder.getView(R.id.img_recycler);
                switch (img.size()) {
                    case 0:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.GONE);


                        break;
                    case 1:
                        status1.setVisibility(View.VISIBLE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.GONE);
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
                                img1);
                        img1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(InstallDetails22Activity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 0);
                                startActivity(intent);
                            }
                        });
                        break;
                    case 2:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.VISIBLE);
                        status3.setVisibility(View.GONE);
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
                                img2_1);
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(1),
                                img2_2);
                        img2_1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(InstallDetails22Activity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 0);
                                startActivity(intent);
                            }
                        });
                        img2_2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(InstallDetails22Activity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 1);
                                startActivity(intent);
                            }
                        });
                        break;
                    default:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.VISIBLE);
                        img_recycler.setLayoutManager(new GridLayoutManager(InstallDetails22Activity.this, 3, LinearLayoutManager.VERTICAL, false));
                        img_recycler.setAdapter(new BaseRVAdapter(InstallDetails22Activity.this, img) {
                            @Override
                            public int getLayoutId(int viewType) {
                                return R.layout.item_photo;
                            }

                            @Override
                            public void onBind(BaseViewHolder holder, final int position) {
                                holder.getImageView(R.id.img).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(InstallDetails22Activity.this, ShowImageDetail.class);
                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                        intent.putExtra("index", position);
                                        startActivity(intent);
                                    }
                                });
                                ImageLoader.getInstance().displayImage(img.get(position), holder.getImageView(R.id.img));
                            }

                        });

                        break;
                }

            }

        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.look:
            case R.id.look2:
                startActivity(new Intent(this, LookComActivity.class).putExtra("pid", pid).putExtra("type", type));
                break;
            case R.id.select_project:
                Intent intent2 = new Intent(this, SelectXiangmu.class);
                startActivityForResult(intent2, 999);
                break;
            case R.id.reduce:
                if (num <= 0) {
                    MyToast("再减就没了");
                    return;
                }
                num--;
                shop.setText(num + "");

                break;
            case R.id.ll_add:
                num++;
                shop.setText(num + "");
                break;
            case R.id.btn:
                if (MyApplication.getInstance().getUserBean() == null) {
                    MyToast("请登录");
                    startActivity(new Intent(getApplicationContext(), LoginFirstActivity.class));
                    return;
                }
                if ("5".equals(pid))
                    if ("".equals(budgetid) || null == budgetid) {
                        MyToast("请选择项目");
                        return;
                    }
                try {
                    Intent intent = new Intent(this, InstallYuYue3Activity.class);
                    intent.putExtra("danwei", info1.getUnit());
                    intent.putExtra("type", "0").putExtra("pid", pid).putExtra("type", info1.getType());
                    intent.putExtra("price", info1.getPrice());
                    if ("5".equals(pid)) {
                        intent.putExtra("budgetid", budgetid);
                        intent.putExtra("name", project_name.getText());

                    }
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

        }
    }

    private String budgetid = "";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 999:
                if (resultCode == RESULT_OK) {
                    //  selectId = data.getStringExtra("selectId");
                    project_name.setText("" + data.getStringExtra("selectName"));
                    String name = data.getStringExtra("name");
                    budgetid = data.getStringExtra("id");
                    project_name.setText(name + "");

                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
